package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.generator.dao.TbUserMovieCollectionDao;
import io.renren.modules.generator.entity.TbUserMovieCollectionEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.TbMovieDao;
import io.renren.modules.generator.entity.TbMovieEntity;
import io.renren.modules.generator.service.TbMovieService;


@Service("tbMovieService")
public class TbMovieServiceImpl extends ServiceImpl<TbMovieDao, TbMovieEntity> implements TbMovieService {

    @Autowired
    private TbMovieDao tbMovieDao;
    @Autowired
    private TbUserMovieCollectionDao tbUserMovieCollectionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper qw = new QueryWrapper<TbMovieEntity>();
        if (params.get("year") != null) {
            qw.eq("year", params.get("year"));
        }
        if (params.get("typeId") != null) {
            qw.eq("type_id", params.get("typeId"));
        }
        if (params.get("movieName") != null) {
            qw.like("movie_name", params.get("movieName"));
        }

        IPage<TbMovieEntity> page = this.page(
                new Query<TbMovieEntity>().getPage(params),
                qw
        );

        return new PageUtils(page);
    }

    @Override
    public List recommendList(UserEntity userEntity) {

        QueryWrapper<TbMovieEntity> qw = new QueryWrapper<TbMovieEntity>();

        qw.orderByDesc("movie_score");

        // 登录了，获取电影的类型，根据类型查询
        if (userEntity != null && userEntity.getUserId() != null) {
            QueryWrapper<TbUserMovieCollectionEntity> umcQw = new QueryWrapper<>();
            umcQw.eq("user_id", userEntity.getUserId());
            umcQw.orderByDesc("id");
            List<TbUserMovieCollectionEntity> userMovieCollectionList = tbUserMovieCollectionDao.selectList(umcQw);
            if (CollectionUtils.isNotEmpty(userMovieCollectionList)) {
                TbUserMovieCollectionEntity m1 = userMovieCollectionList.get(0);
                qw.eq("type_id", m1.getType());
                System.out.println("通过user匹配推荐..");
            }
        }

        IPage<TbMovieEntity> page = this.page(
                new Query<TbMovieEntity>().getPage(new HashMap<>()),
                qw
        );
        return new PageUtils(page).getList();
    }

}